package com.chookss.home.workbench;

import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.ImageView;
import android.widget.LinearLayout;

import androidx.fragment.app.Fragment;
import androidx.viewpager.widget.ViewPager;

import com.chookss.MyApp;
import com.chookss.R;
import com.chookss.home.entity.ContactEntity;
import com.chookss.mine.MyOrderPagerAdapter;
import com.chookss.tools.MyEvent;
import com.chookss.tools.NavigationTools;
import com.chookss.tools.Utils;
import com.chookss.view.NoScrollViewPager;
import com.lvgroup.hospital.base.BaseAct;

import org.greenrobot.eventbus.Subscribe;
import org.greenrobot.eventbus.ThreadMode;
import org.jetbrains.annotations.Nullable;

import java.io.Serializable;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;

public class WorksActivity extends BaseAct {


    @BindView(R.id.llShaixuan)
    LinearLayout llShaixuan;
    @BindView(R.id.llBatch)
    LinearLayout llBatch;
    @BindView(R.id.llAudit)
    LinearLayout llAudit;
    @BindView(R.id.ivPlDelete)
    ImageView ivPlDelete;
    @BindView(R.id.llAudited)
    LinearLayout llAudited;
    @BindView(R.id.viewPager)
    NoScrollViewPager viewPager;

    private MyOrderPagerAdapter pagerAdapter;
    private WorkFragment logFragment0;
    private WorkFragment logFragment1;
    private ArrayList<Fragment> fragments = new ArrayList<>();
    private int position = 0;//默认选择待审核1已审核

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_works);
        ButterKnife.bind(this);

    }

    @Override
    public void initData() {
        initState();
        NavigationTools.saveUseRecord(this,"2","09");
        logFragment0 = new WorkFragment("0");
        logFragment1 = new WorkFragment("1");
        fragments.add(logFragment0);
        fragments.add(logFragment1);
        pagerAdapter = new MyOrderPagerAdapter(getSupportFragmentManager(), fragments);
        viewPager.setAdapter(pagerAdapter);
        viewPager.setOnPageChangeListener(new ViewPager.OnPageChangeListener() {
            @Override
            public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {

            }

            @Override
            public void onPageSelected(int positions) {
                position = positions;
                changeUi();
            }

            @Override
            public void onPageScrollStateChanged(int state) {

            }
        });
        viewPager.setNoScroll(true);
        changeUi();

    }


    private void changeUi() {
        if(viewPager==null){
            return;
        }
        viewPager.setCurrentItem(position);
        if (position == 0) {
            llAudit.getChildAt(0).setSelected(true);
            llAudit.getChildAt(1).setVisibility(View.VISIBLE);
            llAudited.getChildAt(0).setSelected(false);
            llAudited.getChildAt(1).setVisibility(View.INVISIBLE);
            llBatch.setVisibility(View.VISIBLE);
        } else {
            llAudit.getChildAt(0).setSelected(false);
            llAudit.getChildAt(1).setVisibility(View.INVISIBLE);
            llAudited.getChildAt(0).setSelected(true);
            llAudited.getChildAt(1).setVisibility(View.VISIBLE);
            llBatch.setVisibility(View.INVISIBLE);
        }
    }

    private boolean batchShow = false;//是否显示批量信息
    @OnClick({R.id.common_title_back, R.id.llShaixuan, R.id.llBatch, R.id.llAudit, R.id.llAudited})
    public void onClick(View view) {
        switch (view.getId()) {
            case R.id.common_title_back:
                finish();
                break;
            case R.id.llShaixuan:
                Intent intent = new Intent(this, WorkChoiceActivity.class);
                intent.putExtra("startTime", startTime);
                intent.putExtra("endTime", endTime);
                intent.putExtra("position", position);
                intent.putExtra("pass", pass);
                intent.putExtra("selectList", (Serializable) selectList);
                startActivityForResult(intent, 1);
                break;
            case R.id.llBatch:
                if(ivPlDelete==null){
                    return;
                }
                logFragment0.batchData();
                if(batchShow){
                    batchShow=false;
                    ivPlDelete.setBackgroundResource(R.drawable.pl_delete);
                }else{
                    batchShow=true;
                    ivPlDelete.setBackgroundResource(R.drawable.pl_delete_choice);
                }
                break;
            case R.id.llAudit:
                position = 0;
                changeUi();
                break;
            case R.id.llAudited:
                position = 1;
                changeUi();
                break;

        }
    }
    private String startTime = "", endTime = "",pass="";
    private List<ContactEntity> selectList = new ArrayList<>();

    @Override
    protected void onActivityResult(int requestCode, int resultCode, @androidx.annotation.Nullable Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        if (requestCode == 1) {
            if (data != null) {
                if(logFragment0==null||logFragment1==null){
                    return;
                }
                startTime = data.getStringExtra("startTime");
                endTime = data.getStringExtra("endTime");
                pass = data.getStringExtra("pass");
                selectList = (List<ContactEntity>) data.getSerializableExtra("data");
                if (position == 0) {
                    logFragment0.refreshData(startTime, endTime,"", selectList);
                } else {
                    logFragment1.refreshData(startTime, endTime,pass, selectList);
                }
            }
        }
    }
    @Subscribe(threadMode = ThreadMode.MAIN)
    public void onMoonEvent(MyEvent messageEvent) {
        //事件订阅者处理事件
        if (messageEvent.getHashMap() != null) {
            Map<String, String> map = messageEvent.getHashMap();

        } else {
            String msg = messageEvent.getMessage();
            if (Utils.isNull(msg)) {
                return;
            }
            if (msg.equals("RefreshWork")) {
                batchShow=false;
                ivPlDelete.setBackgroundResource(R.drawable.pl_delete);
            }
        }
    }
}
